//
//  ShellWallertViewModel.m
//  YGXWallert
//
//  Created by lvzhao on 2019/5/15.
//  Copyright © 2019 链动科技. All rights reserved.
//

#import "ShellWallertViewModel.h"
@interface ShellWallertViewModel()

@property (nonatomic,assign) NSInteger pageIndex;

@end
@implementation ShellWallertViewModel


- (instancetype)init
{
    self = [super init];
    if (self) {
        self.currentCurrncy = @"USDT";
    }
    return self;
}

/**
 Convert 我的钱包
 */
- (void)getUserAllCoin{
    
    @weakify(self);
    self.wallertModel = [[ShellWallertModel alloc] init];
    self.wallertModel.currencyType = ShellCurrencyTypeETH;
    self.wallertModel.coinCode = self.currentCurrncy;
    [LZNetworkingManager lz_request:@"get" url:kReadWalletInfo params:@{@"coinCode":self.currentCurrncy} success:^(id  _Nullable responseObject) {
        dismiss(LZTool.currentViewController.view);
        @strongify(self);
        if([responseObject[kResultStatus] isEqualToString:kCode]){
            self.wallertModel = [ShellWallertModel mj_objectWithKeyValues:responseObject[kResultData]];
            if([self.currentCurrncy isEqualToString:@"USDT"]){
                self.wallertModel.currencyType = ShellCurrencyTypeETH_USDT;
            } else if([self.currentCurrncy isEqualToString:@"ETH"]){
                self.wallertModel.currencyType = ShellCurrencyTypeETH;
            } else if([self.currentCurrncy isEqualToString:@"BTC"]){
                self.wallertModel.currencyType = ShellCurrencyTypeBTC;
            }
        }
        [self.reloadSubject sendNext:@(3)];
        
    } failure:^(NSError * _Nullable error) {
        dismiss(LZTool.currentViewController.view);
        @strongify(self);
        [self.reloadSubject sendNext:@(3)];
        
    } isFailTip:YES];
    
    //获取个人信息
     [LZNetworkingManager lz_request:@"get" url:kUserInfo params:nil success:^(id  _Nullable responseObject) {
         if([responseObject[kResultStatus] isEqualToString:kCode]){
             NSDictionary *dataDictionary = responseObject[kResultData];
             ShellUserDetailModel *infoModel = [LZToolCache getUserInfo];
             infoModel.payPasswordFlag = [dataDictionary[@"payPasswordFlag"] boolValue];
             [LZToolCache saveUserInfo:infoModel];
         }
     } failure:^(NSError * _Nullable error) {}];
    
    
}

/**
 Convert 钱包记录
 @param  isFirst 是否是第一次
 */
- (void)getWallertRecord:(BOOL)isFirst{
    
    if (isFirst) {
        self.pageIndex = 1;
        [self.dataArray removeAllObjects];
    }
    
    NSString *url = [NSString stringWithFormat:@"%@/%ld",kQueryWalletDetail,(long)self.pageIndex];
    @weakify(self);
    [LZNetworkingManager lz_request:@"get" url:url params:@{}success:^(id  _Nullable responseObject) {
        @strongify(self);
        NSArray *dataArray = responseObject[kResultData];
        
        if (isFirst) {
            [self.dataArray removeAllObjects];
        }
        for (int i = 0; i < dataArray.count; i++) {
            ShellWallertModel *wallertModel = [ShellWallertModel mj_objectWithKeyValues:dataArray[i]];
            [self.dataArray addObject:wallertModel];
        }
        //分页查询没有更多数据, 或者是当前页面  ==  后台返回的总页数
        NSInteger totalCount = [responseObject[@"total"] integerValue];
        if(self.dataArray.count == totalCount){
            //没有更多数据
            [self.reloadSubject sendNext:@(0)];
            return;
        }
        //看是否有数据
        if(self.dataArray.count == 0){
            [self.reloadSubject sendNext:@(-1)];
        } else {
            self.pageIndex++;
            [self.reloadSubject sendNext:@(1)];
        }
        
    } failure:^(NSError * _Nullable error) {
        
        [self.reloadSubject sendNext:@(0)];
        
    } notwork:^(NSError * _Nullable error) {
        [self.reloadSubject sendNext:@(0)];
        
    } isLoading:NO isFailTip:YES];

}
@end
